package com.all.play.mymd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.mymd.R;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/5/11.
 * Time: 2018/5/11  11:23
 */

public class RefreshAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<String> mDatas;
    private final LayoutInflater layoutInflater;

    private static final int TYPE_ITEM   = 0;
    private static final int TYPE_FOOTER = 1;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE     = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE     = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;

    public RefreshAdapter(Context context, List<String> mDatas) {

        this.context = context;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = View.inflate(parent.getContext(), R.layout.item, null);

        if (viewType==TYPE_ITEM){
            View view = layoutInflater.inflate(R.layout.item, parent,false);
            return new ItemViewHolder(view);
        }else if (viewType==TYPE_FOOTER){
            View footview = layoutInflater.inflate(R.layout.loade_more, parent, false);
            return new FooterViewHolder(footview);
        }

        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder= (ItemViewHolder) holder;
            itemViewHolder.tv_item.setText(mDatas.get(position));
        }else if(holder instanceof FooterViewHolder){
            FooterViewHolder footerViewHolder= (FooterViewHolder) holder;

            switch(mLoadMoreStatus){
                case PULLUP_LOAD_MORE:
                    footerViewHolder.mTvLoadText.setText("上拉刷新...");
                    break;
                case LOADING_MORE:
                    footerViewHolder.mTvLoadText.setText("正在加载...");
                    break;
                case NO_LOAD_MORE:
                    footerViewHolder.mLoadLayout.setVisibility(View.GONE);
                    break;

                default:

                    break;
            }
        }


    }

    @Override
    public int getItemCount() {
        //RecyclerView的count设置为数据总条数+ 1（footerView）
        return mDatas==null?0:mDatas.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        //最后一个item设置为footerView
        if (position+1==getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }

    public void addHeadItem(List<String> headDatas) {
        mDatas.addAll(0,headDatas);
        notifyDataSetChanged();
    }

    public void addFootItem(List<String> footerDatas) {
        mDatas.addAll(footerDatas);
        notifyDataSetChanged();
    }

    /**
     * 更新加载更多状态
     * @param state
     */
    public void changeMoreState(int state){
        mLoadMoreStatus=state;
        notifyDataSetChanged();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView tv_item;
        public ItemViewHolder(View itemView) {
            super(itemView);

            tv_item=itemView.findViewById(R.id.tvContent);

            initClickListen(itemView);
        }

        private void initClickListen(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    
    public class FooterViewHolder extends RecyclerView.ViewHolder{

        ProgressBar mPbLoad;
        TextView mTvLoadText;
        LinearLayout mLoadLayout;
        public FooterViewHolder(View itemView) {
            super(itemView);
            mPbLoad= itemView.findViewById(R.id.pbLoad);
            mTvLoadText = itemView.findViewById(R.id.tvLoadText);
            mLoadLayout = itemView.findViewById(R.id.loadLayout);
        }
    }
}
